<?php /* УТФ-8:fix */

class SPIcontroller__manage extends SPIcontroller__dummy
{
    public function exec()
    {
        SPI::I('lang')->set_default('ru');

        SPI::I('Iam')->login(
            SPI::I('var')->get('admin_login:login'),
            SPI::I('var')->get('admin_login:password')
        );

        if( SPI::I('Iam')->is_permit( SPI_IAM_PERM_ADMIN ) || 
            SPI::I('Iam')->is_permit( SPI_IAM_PERM_DJ ) )
        {
            $this->exec_manage();
        }
        else
        {
            $this->exec_login();
        }

        return true;
    }

    private function exec_login()
    {
        $content = array();
        $content['link_minify_css']     = SPIc::I('manage')->link->minify('css');
        $content['link_minify_js']      = SPIc::I('manage')->link->minify('js');

        $content['login']               = SPI::I('var')->get('admin_login:login');

        $content['last_login_status']   = SPI::I('Iam')->get_last_login_status();
        $content['last_login_message']  = SPI::I('Iam')->get_last_login_message();

        SPIv::I('manage')->render_login( $content );
        return true;
    }

    private function exec_manage()
    {
        $content = array();
        $content['link_minify_css']     = SPIc::I('manage')->link->minify('css');
        $content['link_minify_js']      = SPIc::I('manage')->link->minify('js');
        $content['link_manage_home']    = $this->pack_link();

        $content['menu'] = SPIc::I('manage')->menu->build();

        $topic = SPI::I('var')->get('topic');

        if( $topic == '' || $topic == $this->CONTROLLER_ID /* self */ )
        {
            if( SPI::I('var')->get('open_toolbar') === false )
            {
                SPIc::I('manage')->welcome->exec( $content );
            }
        }
        else
        {
            $cObject = SPIc::I($topic);

            if( $cObject !== false )
            {
                if( method_exists( $cObject, 'exec' ) === true )
                {
                    $cObject->exec( $content );
                }
            }
        }

        SPIv::I('manage')->render( $content );

        return true;
    }

    public function pack_link( $vars = null )
    {
        return SPIc::I('manage')->link->pack( $this->CONTROLLER_ID, null, $vars );
    }
}
